package business

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/business"
	businessReq "github.com/flipped-aurora/gin-vue-admin/server/model/business/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"gorm.io/gorm"
)

type EnterpriseService struct {
}

// CreateEnterprise 创建企业记录
// Author [piexlmax](https://github.com/piexlmax)
func (enterpriseService *EnterpriseService) CreateEnterprise(enterprise *business.Enterprise) (err error) {
	err = global.GVA_DB.Create(enterprise).Error
	return err
}

// DeleteEnterprise 删除企业记录
// Author [piexlmax](https://github.com/piexlmax)
func (enterpriseService *EnterpriseService) DeleteEnterprise(enterprise business.Enterprise) (err error) {
	db := global.GVA_DB.Model(&business.Enterprise{}).Where("id = ?", enterprise.ID)
	if enterprise.UserId != 0 {
		db = db.Where("user_id = ? ", enterprise.UserId)
	}
	err = db.Delete(&enterprise).Error
	return err
}

// DeleteEnterpriseByIds 批量删除企业记录
// Author [piexlmax](https://github.com/piexlmax)
func (enterpriseService *EnterpriseService) DeleteEnterpriseByIds(ids request.IdsReq, userId uint) (err error) {
	db := global.GVA_DB.Model(&business.Enterprise{}).Where("id in (?)", ids)
	if userId != 0 {
		db.Where("user_id = ? ", userId)
	}
	err = global.GVA_DB.Delete(&business.Enterprise{}).Error
	return err
}

// UpdateEnterprise 更新企业记录
// Author [piexlmax](https://github.com/piexlmax)
func (enterpriseService *EnterpriseService) UpdateEnterprise(enterprise business.Enterprise) (err error) {
	enterpriseMap := map[string]interface{}{
		"name":        enterprise.Name,
		"classify_id": enterprise.ClassifyId,
		"msg":         enterprise.Msg,
	}
	err = global.GVA_DB.Model(&business.Enterprise{}).Where("id = ?", enterprise.ID).Updates(enterpriseMap).Error
	return err
}

// GetEnterprise 根据id获取企业记录
// Author [piexlmax](https://github.com/piexlmax)
func (enterpriseService *EnterpriseService) GetEnterprise(id uint, userId uint) (enterprise business.Enterprise, err error) {
	db := global.GVA_DB.Where("id = ? ", id)
	if userId != 0 {
		db = db.Where("user_id = ? ", userId)
	}
	err = db.First(&enterprise).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	return
}

// GetEnterpriseByUserId 根据user_id获取企业记录
// Author [piexlmax](https://github.com/piexlmax)
func (enterpriseService *EnterpriseService) GetEnterpriseByUserId(userId uint) (enterprise []business.Enterprise, err error) {
	db := global.GVA_DB.Where("user_id = ? ", userId)
	err = db.Find(&enterprise).Error
	return
}

// GetEnterpriseByName 根据名称获取企业记录
// Author [piexlmax](https://github.com/piexlmax)
func (enterpriseService *EnterpriseService) GetEnterpriseByName(userId uint, name string) (enterprise business.Enterprise, err error) {
	db := global.GVA_DB.Where("user_id = ? and name = ? ", userId, name)
	err = db.First(&enterprise).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	return
}

// GetEnterpriseInfoList 分页获取企业记录
// Author [piexlmax](https://github.com/piexlmax)
func (enterpriseService *EnterpriseService) GetEnterpriseInfoList(info businessReq.EnterpriseSearch) (list []business.Enterprise, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&business.Enterprise{})
	var enterprises []business.Enterprise
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.Name != "" {
		db = db.Where("name LIKE ?", "%"+info.Name+"%")
	}
	if info.ClassifyId != 0 {
		db = db.Where("classify_id = ?", info.ClassifyId)
	}
	if info.UserId != 0 {
		db = db.Where("user_id = ?", info.UserId)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&enterprises).Error
	return enterprises, total, err
}
